<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\MoneyLog;
use app\common\model\option\Log as OptionLog;
use app\common\model\option\Option;
use app\common\model\order\Order;
use app\common\model\red\Log;
use app\common\model\red\Log as RedLog;
use app\common\model\User;
use app\common\model\user\Credit;
use think\Db;
use think\Log as ThinkLog;

/**
 * 定时任务接口
 * @ApiInternal
 */
class Crontab extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    //会员到期取消
    public static function cancel_vip()
    {
        $time = time();
        Db::startTrans();
        try {
            $user = User::all(['level' => 1]);
            foreach ($user as $v) {
                if ($v->validitytime < $time) {
                    $v->level = 0;
                    $v->validitytime = NULL;
                    $v->save();
                    Credit::where('user_id', $v->id)->delete();
                }
            }
            Db::commit();
            echo 'success';
        } catch (\Exception $e) {
            Db::rollback();
            ThinkLog::error($e->getMessage());
            echo 'error';
        }
    }
}
